package com.netease.xone.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;


public abstract class AdapterBaseFragmentPager extends FragmentPagerAdapter {
	private Context mContext;
	private FragmentManager mFragmentManager;
	
	public AdapterBaseFragmentPager(FragmentManager fm, Context context) {
		super(fm);
		mContext = context;
		mFragmentManager = fm;
	}

	/**
	 * 通知FragmentActivity有Fragment生成，目的是为了屏幕切换
	 * 保存FragmentPagerAdapter中的Fragment数据
	 * @param fragment
	 */
	public void onAttachFragment(Fragment fragment) {
		if (mContext instanceof FragmentActivity) {
			((FragmentActivity) mContext).onAttachFragment(fragment);
		}
	}
	
	public void clear() {
		mContext = null;
		mFragmentManager = null;
	}
	
    public Fragment getActiveFragment(ViewPager container, int position) {
        String name = makeFragmentName(container.getId(), position);
        return mFragmentManager.findFragmentByTag(name);
    }
	
	public static String makeFragmentName(int viewId, int index) {
        return "android:switcher:" + viewId + ":" + index;
    }
}
